package net.mingsoft.basic.biz.impl;

import net.mingsoft.base.biz.impl.*;
import net.mingsoft.basic.dao.*;
import net.mingsoft.basic.entity.*;
import net.mingsoft.basic.biz.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;
import org.springframework.beans.factory.annotation.*;
import net.mingsoft.base.dao.*;
import net.mingsoft.basic.bean.*;
import java.util.*;

@Service("cityBizImpl")
@Transactional
public class CityBizImpl extends BaseBizImpl<ICityDao, CityEntity> implements ICityBiz
{
    @Autowired
    private ICityDao cityDao;
    
    protected IBaseDao getDao() {
        return (IBaseDao)this.cityDao;
    }
    
    public List<CityEntity> queryProvince() {
        return this.cityDao.queryProvince();
    }
    
    public List<CityEntity> queryCity(final CityEntity cityEntity) {
        return this.cityDao.queryCity(cityEntity);
    }
    
    public List<CityEntity> queryCounty(final CityEntity cityEntity) {
        return this.cityDao.queryCounty(cityEntity);
    }
    
    public List<CityEntity> queryTown(final CityEntity cityEntity) {
        return this.cityDao.queryTown(cityEntity);
    }
    
    public List<CityEntity> queryVillage(final CityEntity cityEntity) {
        return this.cityDao.queryVillage(cityEntity);
    }
    
    public List<CityBean> queryForTree(final int level, final String type) {
        final List<CityEntity> cityList = this.cityDao.queryByLevel(level);
        final Map<Long, String> province = new HashMap<Long, String>();
        final Map<Long, Map<Long, String>> city = new HashMap<Long, Map<Long, String>>();
        final Map<Long, Map<Long, String>> county = new HashMap<Long, Map<Long, String>>();
        final Map<Long, Map<Long, String>> town = new HashMap<Long, Map<Long, String>>();
        final Map<Long, Map<Long, String>> village = new HashMap<Long, Map<Long, String>>();
        for (final CityEntity cityEntity : cityList) {
            if (level >= 1) {
                if (province.get(cityEntity.getProvinceId()) == null) {
                    province.put(cityEntity.getProvinceId(), cityEntity.getProvinceName());
                }
                if (level < 2) {
                    continue;
                }
                if (city.get(cityEntity.getProvinceId()) != null) {
                    city.get(cityEntity.getProvinceId()).put(cityEntity.getCityId(), cityEntity.getCityName());
                }
                else {
                    final Map<Long, String> tempCity = new HashMap<Long, String>();
                    tempCity.put(cityEntity.getCityId(), cityEntity.getCityName());
                    city.put(cityEntity.getProvinceId(), tempCity);
                }
                if (level < 3) {
                    continue;
                }
                if (county.get(cityEntity.getCityId()) != null) {
                    county.get(cityEntity.getCityId()).put(cityEntity.getCountyId(), cityEntity.getCountyName());
                }
                else {
                    final Map<Long, String> tempCounty = new HashMap<Long, String>();
                    tempCounty.put(cityEntity.getCountyId(), cityEntity.getCountyName());
                    county.put(cityEntity.getCityId(), tempCounty);
                }
                if (level < 4) {
                    continue;
                }
                if (town.get(cityEntity.getCountyId()) != null) {
                    town.get(cityEntity.getCountyId()).put(cityEntity.getTownId(), cityEntity.getTownName());
                }
                else {
                    final Map<Long, String> tempTown = new HashMap<Long, String>();
                    tempTown.put(cityEntity.getTownId(), cityEntity.getTownName());
                    town.put(cityEntity.getCountyId(), tempTown);
                }
                if (level < 5) {
                    continue;
                }
                if (village.get(cityEntity.getTownId()) != null) {
                    village.get(cityEntity.getTownId()).put(cityEntity.getVillageId(), cityEntity.getVillageName());
                }
                else {
                    final Map<Long, String> tempVillage = new HashMap<Long, String>();
                    tempVillage.put(cityEntity.getVillageId(), cityEntity.getVillageName());
                    village.put(cityEntity.getTownId(), tempVillage);
                }
            }
        }
        final List<CityBean> cityBeanList = new ArrayList<CityBean>();
        if ("tree".equals(type)) {
            if (level >= 1) {
                for (final Long provinceKey : province.keySet()) {
                    final CityBean provinceBean = new CityBean();
                    final String provinceName = province.get(provinceKey);
                    provinceBean.setId(provinceKey);
                    provinceBean.setName(provinceName);
                    provinceBean.setChildrensList(new ArrayList<CityBean>());
                    if (level >= 2) {
                        final Map<Long, String> cityMap = city.get(provinceKey);
                        for (final Long cityKey : cityMap.keySet()) {
                            final String cityName = cityMap.get(cityKey);
                            final CityBean cityBean = new CityBean();
                            cityBean.setId(cityKey);
                            cityBean.setName(cityName);
                            cityBean.setChildrensList(new ArrayList<CityBean>());
                            if (level >= 3) {
                                final Map<Long, String> countyMap = county.get(cityKey);
                                for (final Long countyKey : countyMap.keySet()) {
                                    final String countyName = countyMap.get(countyKey);
                                    final CityBean countyBean = new CityBean();
                                    countyBean.setId(countyKey);
                                    countyBean.setName(countyName);
                                    countyBean.setChildrensList(new ArrayList<CityBean>());
                                    if (level >= 4) {
                                        final Map<Long, String> townMap = town.get(countyKey);
                                        for (final Long townKey : townMap.keySet()) {
                                            final String townName = townMap.get(townKey);
                                            final CityBean townBean = new CityBean();
                                            townBean.setId(townKey);
                                            townBean.setName(townName);
                                            townBean.setChildrensList(new ArrayList<CityBean>());
                                            if (level >= 5) {
                                                final Map<Long, String> villageMap = village.get(townKey);
                                                for (final Long villageKey : villageMap.keySet()) {
                                                    final String villageName = villageMap.get(villageKey);
                                                    final CityBean villageBean = new CityBean();
                                                    villageBean.setId(villageKey);
                                                    villageBean.setName(villageName);
                                                    townBean.getChildrensList().add(villageBean);
                                                }
                                            }
                                            countyBean.getChildrensList().add(townBean);
                                        }
                                    }
                                    cityBean.getChildrensList().add(countyBean);
                                }
                            }
                            provinceBean.getChildrensList().add(cityBean);
                        }
                    }
                    cityBeanList.add(provinceBean);
                }
            }
        }
        else {
            CityBean cityBean2 = new CityBean();
            if (level >= 1) {
                for (final Long provinceId : province.keySet()) {
                    cityBean2 = new CityBean();
                    cityBean2.setId(provinceId);
                    cityBean2.setName(province.get(provinceId));
                    cityBeanList.add(cityBean2);
                }
            }
            if (level >= 2) {
                for (final Long provinceId : city.keySet()) {
                    final Map<Long, String> _city = city.get(provinceId);
                    for (final Long cityId : _city.keySet()) {
                        cityBean2 = new CityBean();
                        cityBean2.setId(cityId);
                        cityBean2.setName(_city.get(cityId));
                        cityBean2.setParentId(provinceId);
                        cityBeanList.add(cityBean2);
                    }
                }
            }
            if (level >= 3) {
                for (final Long cityId2 : county.keySet()) {
                    final Map<Long, String> _county = county.get(cityId2);
                    for (final Long countyId : _county.keySet()) {
                        cityBean2 = new CityBean();
                        cityBean2.setId(countyId);
                        cityBean2.setName(_county.get(countyId));
                        cityBean2.setParentId(cityId2);
                        cityBeanList.add(cityBean2);
                    }
                }
            }
            if (level >= 4) {
                for (final Long countyId2 : town.keySet()) {
                    final Map<Long, String> _town = county.get(countyId2);
                    for (final Long townId : _town.keySet()) {
                        cityBean2 = new CityBean();
                        cityBean2.setId(townId);
                        cityBean2.setName(_town.get(townId));
                        cityBean2.setParentId(countyId2);
                        cityBeanList.add(cityBean2);
                    }
                }
            }
            if (level >= 5) {
                for (final Long countyId2 : village.keySet()) {
                    final Map<Long, String> _village = county.get(countyId2);
                    for (final Long villageId : _village.keySet()) {
                        cityBean2 = new CityBean();
                        cityBean2.setId(villageId);
                        cityBean2.setName(_village.get(villageId));
                        cityBean2.setParentId(countyId2);
                        cityBeanList.add(cityBean2);
                    }
                }
            }
        }
        return cityBeanList;
    }
    
    public List<CityEntity> queryByLevel(final int level) {
        return this.cityDao.queryByLevel(level);
    }
    
    public List<CityEntity> queryById(final CityEntity cityEntity) {
        return this.cityDao.queryById(cityEntity);
    }
    
    public CityEntity queryByEntity(final CityEntity city) {
        return this.cityDao.queryByEntity(city);
    }
    
    public List<CityEntity> queryProvinceAndName(final CityEntity city) {
        return this.cityDao.queryProvinceAndName(city);
    }
}
